//
//  SegmentView.swift
//  ShiZhe
//
//  Created by 陈卿伟 on 16/8/25.
//  Copyright © 2016年 陈卿伟. All rights reserved.
//

import UIKit

class SegmentView: UIView {
    
    var scrollView:UIScrollView!//底部滑动控件
    var moreButton:UIButton!//最右边的更多按钮
    var redLine:UIView!//红线
    var itemArr = ["热门","关注","最新","Swift","Java","PHP","Ruby","Objective-C"]
    
    override init(frame: CGRect) {
        scrollView = UIScrollView()
        moreButton = UIButton()
        redLine = UIView()
        super.init(frame: frame)
        self.createScrollView()
        self.createMoreItem()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

//MARK:UI设置
extension SegmentView{
    //创建一个scrollView
    func createScrollView(){
        scrollView.frame = self.frame
        scrollView.showsVerticalScrollIndicator = false
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.scrollEnabled = true
        scrollView.backgroundColor = UIColor.whiteColor()
        self.addSubview(scrollView)
    }
    
    //创建文字选项  没有数据 先模拟创建一下
    func createSegmentItem(){
        var contentSize:CGFloat = 0//记录现在segment的item项的长度
        for i in 0..<itemArr.count{
            
            let segmItem = UIButton()
            segmItem.setTitle("\(itemArr[i])", forState: UIControlState())
            switch itemArr[i] {
            case "热门","关注","最新":
                segmItem.setTitleColor(UIColor.blackColor(), forState: UIControlState())
            default:
                segmItem.setTitleColor(Theme.GrayColor, forState: UIControlState())
            }
            segmItem.setTitleColor(UIColor.redColor(), forState: .Selected)
            segmItem.titleLabel?.font = BaseFunc.getCustomFont(15)
            segmItem.backgroundColor = UIColor.whiteColor()
            segmItem.tag = 10000+i
            self.scrollView.addSubview(segmItem)
            
            //得到item的title的大小
            let itemString = segmItem.titleLabel?.text
            let constraint = CGSize(width: 10000, height: 10000)
            let size = itemString!.boundingRectWithSize(constraint, options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes:NSDictionary(object:UIFont(name: "Heiti SC", size: 15)!, forKey: NSFontAttributeName as NSCopying) as? [String : AnyObject],context: nil)
            
            segmItem.frame = CGRect(x: 16+contentSize,y: 0,width: size.width+20,height: 40)
            contentSize += (size.width+20)
            
            if i == 0{
                segmItem.selected = true
            }
            
        }
        let widthScroll = contentSize+32+40
        if widthScroll <= WidthAPP{
            self.scrollView.contentSize = CGSize(width: WidthAPP+1, height: 0)
        }else{
            self.scrollView.contentSize = CGSize(width: widthScroll, height: 0)
        }
        self.createRedLine()
    }
    
    //创建右边的 更多按钮
    func createMoreItem(){
        moreButton.frame = CGRect(x: WidthAPP-50,y: 0,width: 50,height: 40)
        moreButton.setImage(UIImage(named: "moreClassify"), forState: UIControlState())
        moreButton.backgroundColor = UIColor.whiteColor()
        self.addSubview(moreButton)
    }
    
    
    //创建下面的红线
    func createRedLine(){
        redLine.frame = CGRect(x: 16,y: 36,width: 48,height: 2)
        redLine.backgroundColor = UIColor.redColor()
        scrollView.addSubview(redLine)
    }
}






















